import { type ConfigEnv, defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import * as path from 'node:path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { viteMockServe } from 'vite-plugin-mock'

export default ({ command, mode }: ConfigEnv) => {
  return defineConfig({
    plugins: [
      vue(),
      // 注册配置SVG图标
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      }),
      // 注册配置mock
      viteMockServe({
        // 根据命令和模式来决定是否启用 mock
        enable: command === 'serve' && mode === 'development',
        mockPath: 'mock',
      }),
    ],
    resolve: {
      alias: {
        '@': path.resolve('./src'), // 相对路径别名配置，使用‘@’代替src
      },
    },
    // scss全局变量一个配置
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "@/styles/variable.scss" as *;',
        },
      },
    },
  })
}
